package stu.zhl.letcode;

import stu.zhl.util.ArrayUtils;

/**
 * @author zhanghanlin
 * @ClassName ProfitMaximization
 * @date 2020/1/1 下午10:48
 * @Description 在一串数组中，判断在一次买入卖出的最大利润
 */
public class ProfitMaximization {

    /**
     * 利润计算,暴力计算
     * 挨个计算对应的利润
     *
     * @param desc 输入参数
     * @return
     */
    public int calculateProfitWithEnum(int[] desc) {
        //最大利润
        int maxProfit = 0;

        for (int i = 0, size = desc.length; i < size; i++) {
            for (int j = 1; j < size; j++) {
                int profit = desc[j] - desc[i];
                if (profit > maxProfit) {
                    maxProfit = profit;
                }
            }
        }
        return maxProfit;
    }

    /**
     * @param desc
     * @return
     */
    public int calculateProfit(int[] desc) {

        int maxProfit = 0;

        for (int i = 0, size = desc.length; i < size; i++) {
            int min = ArrayUtils.min(desc, 0, i);
            int profit = desc[i] - min;
            if (profit > maxProfit) {
                maxProfit = profit;
            }
        }

        return maxProfit;
    }


}
